package 并发编程13.wait方式和park方式堵塞线程;

import java.util.concurrent.locks.LockSupport;

/**
 * @author idea
 * @data 2019/2/1
 */
public class Demo2 {

    public static void main(String[] args) throws Exception {
        TestThread2 thread = new TestThread2( Thread.currentThread() );
        long start = System.nanoTime();
        thread.start();
        try {
            Thread.sleep( 1000 );
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println( "before park" );
        // 等待获取许可 堵塞线程 顺序结构不会被打乱，没有被堵塞 不会被打乱顺序
        LockSupport.park( "Park" );
        System.out.println( "after park:" + (System.nanoTime() - start) );
    }
}


class TestThread2 extends Thread {
    private Object object;
    public TestThread2(Object object) {
        this.object = object;
    }

    @Override
    public void run() {
        System.out.println( "before unpark" );
        //唤醒线程
        LockSupport.unpark( (Thread) object );
        System.out.println( "after unpark" );
    }
}
